home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / ANTENNA / YAGIU112 / RANDOM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-07  |  2.4 KB  |  129 lines

  1. /* I got fed up with evey compiler system using a different name for the
  2. random number generator(s). I've use two random number generators in 
  3. th main code:
  4.  
  5. 1) randreal() Expects a double in range 0 to 1.
  6. 2) randint()  Expects an integer in range 0 to 2^15-1.
  7.  
  8. DONT change the names 'randreal' and 'randint' in all the other souces
  9. file. But DO edit the source of the two functions 'randreal' and
  10. 'randint' in this C file. Let them call whatever you like/works on your
  11. system. 
  12.  
  13. If you use another system (eg VAX), I suggest you put this in functions
  14. randreal and reandint:
  15.  
  16. randint()
  17. {
  18. #ifdef VAX
  19. return(VAX_integer_random_number_generator());
  20. #endif
  21. }
  22.  
  23.  
  24. */
  25.  
  26. #ifdef i386
  27. #ifdef UNIX
  28. #include <time.h>
  29. #endif
  30. #endif
  31. #ifdef DOS
  32. #include <sys/time.h>
  33. #endif
  34. #ifdef sun
  35. #include <sys/time.h>
  36. #endif
  37. #include <stdio.h>
  38. #include <math.h>
  39. /* #include <random.h> */
  40. #ifndef GCC
  41. #include <stdlib.h>
  42. #endif
  43. #include <values.h>
  44. #include "yagi.h"
  45.  
  46. double drand48();
  47. int lrand48();
  48.  
  49. double randreal(void)
  50. {
  51. #ifdef GCC /* GCC on PC running DOS */ 
  52. #ifdef DOS
  53.     return(rand()/65535.0);
  54. #endif
  55. #endif
  56.  
  57. #ifdef i386   /*GCC on my PC running unix */
  58. #ifndef DOS
  59.     return(drand48());
  60. #endif
  61. #endif
  62.  
  63. #ifdef sun
  64.     return(drand48());
  65. #endif
  66. }
  67. double randnorm(void)
  68. {
  69.     static short paired=0 ;
  70.     static double second ;
  71.     double x,y,rad,fac ;
  72.  
  73.     if (paired=!paired)
  74.     {
  75.         do {
  76.         x=2.0*randreal()-1.0 ;
  77.         y=2.0*randreal()-1.0 ;
  78.         rad=x*x+y*y ;
  79.         } while((rad>1.0)||(rad=0.0));
  80.         fac=sqrt(2.0*log(rad)/rad) ;
  81.         second=(x*fac) ;
  82.         paired=!paired ;
  83.         return (y*fac) ;
  84.     } else return (second);
  85. }       
  86.         
  87.         
  88. int randint(void)
  89. {
  90. #ifdef UNIX
  91. #ifdef i386
  92.     return( (int) lrand48()/65535 );
  93. #endif
  94. #endif
  95. #ifdef DOS
  96. #ifdef GCC
  97.     return( (int) rand()/2 );
  98. #endif
  99. #endif
  100.  
  101. #ifdef sun
  102.     return( (int) lrand48()/65535 );
  103. #endif
  104. }
  105.  
  106. /* Since two random number gerators are used (a float and an int type), its
  107. usually necessary to seed both sepparately */
  108. void seedRNG(void)
  109. {
  110.     long seed_time;
  111.  
  112.     time(&seed_time); /* set seed_time to the number of seconds since Jan 1980 */
  113. #ifdef DOS
  114.     srand(seed_time); /* Seed the float type */
  115.     srand(seed_time); /* Seed the int type */
  116. #endif
  117.  
  118. #ifdef UNIX
  119. #ifdef i386
  120.     srand48(seed_time);  /* Seed the float type */
  121.     lrand48(seed_time); /* Seed the int type */
  122. #elif sun
  123.     srand48(seed_time);  /* Seed the float type */
  124.     lrand48(seed_time); /* Seed the int type */
  125. #endif
  126. #endif
  127.  
  128. }
  129.